<%@page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,deweb.cimaplanning.*" errorPage="" %>
<%boolean child = false;%>
<%@include file="validateSessionAJAX.jsp"%>

<%@page import="java.text.DecimalFormat"%><html>
<head>
<title>..:: Sistema ::..</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="deWeb.css" type="text/css" title="deWeb CSS">
<script language="javascript" src="javascript/deweb.js"></script>
<script language="javascript" src="javascript/ajax.js"></script>
<script language="javascript" src="javascript/sort.js"></script>
<script language="javascript" src="javascript/tables.js"></script>
<script language="JavaScript" type="text/JavaScript">

	function recalcularVersion(){
		document.forms[0].recalculate_now.text = "YES";
	}
<!--

//local functions
var theForm		= null;
var theFilters	= null;
var page		= null;




//first function onLoad, modify as needed but do not delete the function
function onLoad(){
	theForm		= document.frmMain;
	theFilters	= document.frmFilters;
	page	= '<%=misc.getPageNoExt(request.getRequestURI())%>';
	
}


//-->
</script>
</head>

<body onLoad="javascript:onLoad();" onFocus="javascript:keep_child_on_top();">
dia: 101201
<br>version: 0.2
<%


boolean DEBUG = false;
if (DEBUG) out.println("<br>DEBUG ON: [" + request.getRequestURI() + "]");

boolean read = true;//seg.getPermEnt(misc.APP, misc.M_CATALOG, misc.S_CASETA, misc.E_CASETA, com.deweb.acpmodseg.ACPModSeg.PERM_LECTURA);
boolean edit = true;//seg.getPermEnt(misc.APP, misc.M_CATALOG, misc.S_CASETA, misc.E_CASETA, com.deweb.acpmodseg.ACPModSeg.PERM_ADMIN);

if (DEBUG) out.println("<br>Permiso lectura: [" + read + "]");
if (DEBUG) out.println("<br>Permiso administracion: [" + edit + "]");

if (!read){
	out.println("No tiene permiso de acceso a esta Ã¡rea");
	out.println("<br><a href=\"javascript:history.back();\">Regresar</a>");
	return;
}

com.deweb.GetProperties prop;
Connection conn 		= null;
Statement stmt  		= null;
Statement stmtPlants  	= null;
Statement stmtCornInventories  	= null;
ResultSet rs			= null;
ResultSet rsPlants		= null;

String sql 		= null;


try{
    prop 				= new com.deweb.GetProperties(request);
    conn 				= prop.getDBConnection();
    stmt 				= conn.createStatement();
    stmtPlants 			= conn.createStatement();
    stmtCornInventories = conn.createStatement();
	
	sql = "";
	
	String keyId	= request.getParameter("keyId") == null ? "" : request.getParameter("keyId");
	int frmAction	= request.getParameter("frmAction") == null ? 0 : (request.getParameter("frmAction").toString().length() == 0 ? 0 : Integer.parseInt(request.getParameter("frmAction").toString()));
	boolean print	= request.getParameter("print") == null ? false : (request.getParameter("frmAction").toString().equals("1") ? true : false);
	
	int order	= request.getParameter("order") == null ? 1 : (request.getParameter("order").toString().length() == 0 ? 1 : Integer.parseInt(request.getParameter("order").toString()));
	String dir	= request.getParameter("dir") == null ? misc.ASC : request.getParameter("dir");
	String versionID = request.getParameter("version_id") == null ? "": request.getParameter("version_id");
	String recalculateNow = request.getParameter("recalculate_now") == null ? "": request.getParameter("recalculate_now");
	if (DEBUG) {
		out.println("<br>keyId: [" + keyId + "]");
		out.println("<br>frmAction: [" + frmAction + "]");
		out.println("<br>order: [" + order + "]");
		out.println("<br>dir: [" + dir + "]");
	} 
	
	sql = "Select * from versions";
	ResultSet rsVersions = stmt.executeQuery(sql);
	
	
	%>
	
	<form action="" method="get">
	<input type="hidden" name="recalculate_now" value="NO">
	<table>
		<tr>
			<td>Version</td>
			<td> 
				<select name="version_id">
					<option SELECTED value="">Seleccione una opci&oacute;n</option>
<%	
	while (rsVersions.next()){
		out.println(" <option value=\""+ rsVersions.getString("version_id") + "\" " + (versionID.equals(rsVersions.getString("version_id")) ? "selected" : "") + ">"+ rsVersions.getString("short_name") + " - " + rsVersions.getString("description") +"</option>");
//		out.println(" <option value=\""+ rsVersions.getString("version_id") + "\" selected>"+ rsVersions.getString("short_name") + " - " + rsVersions.getString("description") +"</option>");
	}
	rsVersions.close();
	
%>					
				</select>
			</td>
			<td >
				 <input type="button" value="Refrescar"  onclick="javascript:document.forms[0].submit();">
				 <input type="button" value="Recalcular" onClick="javascript:document.forms[0].recalculate_now.value='YES'; document.forms[0].submit();">
			</td>
		</tr>
	</table>
	</form>	
	<% 
	
	
	
	if (recalculateNow.equals("YES")){
		sql = "execute [sp_version_distribution_planning] " + versionID;
		stmt.executeUpdate(sql);
	}
	if (versionID.length() == 0) return;
	sql  = " Select distinct * "; 
	sql  +=	" 	from vw_monthly_version_distribution2 "; 
	sql  +=	" 	where version_id = " + versionID;
	sql  +=	" 	order by current_year, current_month, po_year,po_month,biweek, code, code_number, plant_short_name";

	
  if (DEBUG) out.println("<br>sql: [" + sql + "]");
    rs = stmt.executeQuery(sql);
	
	int setOrder = 0;
	boolean odd = false;
	int counter	= 0; 
	
	String id 	= "";
	String dsc	= "";
	
    DecimalFormat df = new DecimalFormat("#,##0.00");



	
	int prevYear  = 0;
	int prevMonth = 0;
	String prevPONum = "";
	String prevBiweekShortName = "";
	double[] plantsDistributed = new double[3];
	String[][] plantsHeader  = new String [10][4];
	String[][] cornDistribution  = new String [4][4];

	double POCapacity =0;
	double POInitial =0;
	double PODistributed = 0;
	double POBlocked = 0;
	boolean completeCurrentTable = false;
	out.println("                    	<table border=\"1\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\" id=\"tblEnc\" >");
    out.println("                        <tr class=\"colHeader\">");
    out.println("                         <td>");
	out.println("                    		<table border=\"1\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\" id=\"tblEnc\" >");
    out.println("                            <tr class=\"colHeader\">");
    out.println("                              <td width=\"300\">PO</td>");
    out.println("                              <td width=\"100\">Inv.&nbsp;Inicial</td>");
    out.println("                              <td width=\"100\">Compras</td>");
    out.println("                              <td width=\"100\">Disponible</td>");
    out.println("                  			   <td width=\"100\">PPA</td>");
    out.println("                              <td width=\"100\">PSJ</td>");
    out.println("                              <td width=\"100\">PTL</td>");
    out.println("                              <td  NOWRAP>Inv.&nbsp;Final</td>");
    out.println("                            </tr>");
    odd = false;
    
	while (rs.next()) {
		odd = !odd;
		counter++;
		String biweekShortName = rs.getString("short_name");
		String poNum = rs.getString("Code").trim()+ "000".substring(1, 4 - rs.getString("code_number").length()) + rs.getString("code_number");
		if ( ((prevMonth != rs.getInt("current_month")) || (prevYear != rs.getInt("current_year"))) && (prevMonth == 0)){
			out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
			out.println("                  <td align=\"center\" colspan=\"8\">"+ rs.getInt("current_year") +" - " + rs.getInt("current_month") +"</td>");
			out.println("                </tr>");
			odd = !odd;
			
			sql = "select v.version_id,mp.[year],mp.[month], p.short_name as plant_short_name,p.description as plant_desciption, pv.*, ISNULL(vpvg.total_grinded,0) as total_grinded";
			sql += " FROM 		versions v ";
			sql += " 			inner join dbo.months_periods_versions	mpv	on mpv.version_id = v.version_id"; 
			sql += " 			inner join dbo.plants_versions	pv	on mpv.month_period_version_id = pv.month_period_version_id"; 
			sql += " 			inner join dbo.plants				p	on p.plant_id = pv.plant_id";
			sql += " 			inner join dbo.months_periods			mp	on mp.month_period_id = mpv.month_period_id";
			sql += " 			inner join dbo.vw_plant_version_grind		vpvg	on vpvg.plant_version_id = pv.plant_version_id";
			sql += " where v.version_id = " + versionID; 
			sql += " 	and mp.[year] = " 	+ rs.getInt("current_year"); 
			sql += " 	and mp.[month] = " 	+ rs.getInt("current_month");
			rsPlants = stmtPlants.executeQuery(sql);
			plantsHeader  = new String [10][4];
			int plantsArrayId =0;

			plantsHeader [0][0] = "Programa de molienda"; 
			plantsHeader [1][0] = "Días molienda Inv. Ini.";
			plantsHeader [2][0] = "Almacen";
			plantsHeader [3][0] = "Inv. Inicial Plantas";
			plantsHeader [4][0] = "Inv. Final Plantas";
			plantsHeader [5][0] = "Max Req";
			plantsHeader [6][0] = "Distribuido";
			plantsHeader [7][0] = "Molido";
			plantsHeader [8][0] = "Descarga";
			while (rsPlants.next()){
				if 	(rsPlants.getString("plant_short_name").trim().equals("PPA")) plantsArrayId = 1;
				if 	(rsPlants.getString("plant_short_name").trim().equals("PSJ")) plantsArrayId = 2;
				if 	(rsPlants.getString("plant_short_name").trim().equals("PTL")) plantsArrayId = 3;
				plantsHeader [0][plantsArrayId] = df.format(rsPlants.getDouble("grind") ); 
				plantsHeader [1][plantsArrayId] = df.format(rsPlants.getDouble("grind_protection_days"));
				plantsHeader [2][plantsArrayId] = df.format(rsPlants.getDouble("storage_amount"));
				plantsHeader [3][plantsArrayId] = df.format(rsPlants.getDouble("start_inventory"));
				plantsHeader [4][plantsArrayId] = df.format(rsPlants.getDouble("end_inventory"));
				plantsHeader [5][plantsArrayId] = df.format(rsPlants.getDouble("grind") +  rsPlants.getDouble("storage_amount") - (rsPlants.getDouble("start_inventory") == 0.0 ? 0.0 :rsPlants.getDouble("start_inventory")));
				plantsHeader [6][plantsArrayId] = df.format(rsPlants.getDouble("received"));
				plantsHeader [7][plantsArrayId] = df.format(rsPlants.getDouble("total_grinded"));
				plantsHeader [8][plantsArrayId] = df.format(rsPlants.getDouble("unload_amount"));

			}
			for (int i = 0; i<9; i++){
				out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
				out.println("                  <td align=\"left\" colspan=\"4\">"+ plantsHeader [i][0] +"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][1]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][2]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][3]+"</td>");
				out.println("                  <td align=\"center\" colspan=\"1\">&nbsp;</td>");				
				out.println("                </tr>");
				odd = !odd;
			}
			out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
			out.println("                  <td align=\"center\" colspan=\"8\">&nbsp;</td>");
			out.println("                </tr>");
			
			/*
				Maiz por planta
			*/
			
			sql = "Select * ";
			sql += " from vw_corn_type_distributions "; 
			sql += " where version_id = " + versionID; 
			sql += " 	and [year] = " 	+ rs.getInt("current_year"); 
			sql += " 	and [month] = " 	+ rs.getInt("current_month");
			sql += " order by [year],[month],corn_type_id,plant_id";
			ResultSet rsCornTypeInv = stmtCornInventories.executeQuery(sql);
				out.println("                <tr class=\"colHeader\">" );
			out.println("                  <td align=\"left\"colspan=\"2\" >Maiz</td>");
			out.println("                  <td align=\"left\"colspan=\"2\" >&nbsp;</td>");
			out.println("                  <td align=\"right\" >PPA</td>");
			out.println("                  <td align=\"right\" >PSJ</td>");
			out.println("                  <td align=\"right\" >PTL</td>");
			out.println("                </tr>" );
			
			odd = true;
			cornDistribution[0][0] = "Inv.&nbsp;Inicial";
			cornDistribution[1][0] = "Distribuido";
			cornDistribution[2][0] = "Molido";
			cornDistribution[3][0] = "Inv. Final";
			String prevCornType = "";
			while (rsCornTypeInv.next()){
				if 	(rsCornTypeInv.getString("short_name").trim().equals("PPA")) plantsArrayId = 1;
				if 	(rsCornTypeInv.getString("short_name").trim().equals("PSJ")) plantsArrayId = 2;
				if 	(rsCornTypeInv.getString("short_name").trim().equals("PTL")) plantsArrayId = 3;
				
				if ( (prevCornType.length() > 0) && (!prevCornType.equals(rsCornTypeInv.getString("code")))){
					
					for (int i = 0; i<4; i++){
						out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
						if (i == 0)
							out.println("                  <td align=\"center\" colspan=\"2\">"+ prevCornType +"</td>");
						else
							out.println("                  <td align=\"center\" colspan=\"2\">&nbsp;</td>");
						out.println("                  <td align=\"left\" colspan=\"2\">"+ cornDistribution [i][0] +"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][1]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][2]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][3]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">&nbsp;</td>");
						out.println("                </tr>");
					}
					odd = !odd;
					
				}
				cornDistribution[0][plantsArrayId] = df.format(rsCornTypeInv.getDouble("start_inventory"));
				cornDistribution[1][plantsArrayId] = df.format(rsCornTypeInv.getDouble("distributed"));
				cornDistribution[2][plantsArrayId] = df.format(rsCornTypeInv.getDouble("grinded"));
				cornDistribution[3][plantsArrayId] = df.format(rsCornTypeInv.getDouble("end_inventory"));
				
				prevCornType =  rsCornTypeInv.getString("code");
			}
			for (int i = 0; i<4; i++){
				out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
				if (i == 0)
					out.println("                  <td align=\"center\" colspan=\"2\">"+ prevCornType +"</td>");
				else
					out.println("                  <td align=\"center\" colspan=\"2\">&nbsp;</td>");
				out.println("                  <td align=\"left\" colspan=\"2\">"+ cornDistribution [i][0] +"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][1]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][2]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][3]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">&nbsp;</td>");
				out.println("                </tr>");
			}
			
			odd = true;

		}
		
		if (prevPONum.length() == 0) {
			prevPONum = poNum;
			prevBiweekShortName = biweekShortName;
		}

		if (!poNum.equals(prevPONum))	{
			out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">");
			out.println("                  <td NOWRAP>" + prevPONum + "&nbsp;" + prevBiweekShortName + "</td>");
			out.println("                  <td align=\"right\">" + (POInitial >0.0 ? df.format(POInitial): "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (POCapacity >0.0 ? df.format(POCapacity): "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (POCapacity + POInitial > 0.0 ? df.format(POCapacity + POInitial) : "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (plantsDistributed[0] > 0.0 ? df.format(plantsDistributed[0]) : "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (plantsDistributed[1] > 0.0 ? df.format(plantsDistributed[1]) : "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (plantsDistributed[2] > 0.0 ? df.format(plantsDistributed[2]) : "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + ( (POCapacity + POInitial)- PODistributed > 0.0 ? df.format((POCapacity + POInitial)- PODistributed) : "&nbsp;") + "</td>");
			out.println("                </tr>");
			odd = !odd;
			plantsDistributed[0] =0;
			plantsDistributed[1] =0;
			plantsDistributed[2] =0;
			POCapacity 	= 0;
			POInitial 	= 0;
			if ((rs.getInt("current_year") == rs.getInt("po_year")) 
			 && (rs.getInt("current_month") == rs.getInt("po_month")))
				POCapacity = (rs.getDouble("capacity") );
			else
				POInitial  = rs.getDouble("capacity");
			POBlocked = rs.getDouble("blocked_qty");
			PODistributed = rs.getDouble("total_distributed_per_month");
		}

//		if  (POCapacity == 0)  POCapacity = (rs.getDouble("capacity") );
			POCapacity 	= 0;
			POInitial 	= 0;
			if ((rs.getInt("current_year") == rs.getInt("po_year")) 
			 && (rs.getInt("current_month") == rs.getInt("po_month")))
				POCapacity = (rs.getDouble("capacity") );
			else
				POInitial  = rs.getDouble("capacity");
		if  (POBlocked == 0)  POBlocked = rs.getDouble("blocked_qty");
		if  (PODistributed == 0)  PODistributed = rs.getDouble("total_distributed_per_month");
		
		
		if 	((rs.getString("plant_short_name").trim().equals("PPA")) 
				&& (rs.getString("plant_distributed").trim().equals("PPA")) 
/*				&& (rs.getInt("current_year") == rs.getInt("po_year")) 
				&& (rs.getInt("current_month") == rs.getInt("po_month")) */) 
			plantsDistributed[0] += rs.getDouble("distributed");
		if 	((rs.getString("plant_short_name").trim().equals("PSJ")) 
				&& (rs.getString("plant_distributed").trim().equals("PSJ")) 
/*				&& (rs.getInt("current_year") == rs.getInt("po_year")) 
				&& (rs.getInt("current_month") == rs.getInt("po_month")) */) 
			plantsDistributed[1] += rs.getDouble("distributed");
		if 	((rs.getString("plant_short_name").trim().equals("PTL")) 
				&& (rs.getString("plant_distributed").trim().equals("PTL")) 
/*				&& (rs.getInt("current_year") == rs.getInt("po_year")) 
				&& (rs.getInt("current_month") == rs.getInt("po_month")) */) 
			plantsDistributed[2] += rs.getDouble("distributed");

		if ( ((prevMonth != rs.getInt("current_month")) || (prevYear != rs.getInt("current_year"))) && (prevMonth >0)){
			
			
          	out.println("                <tr>");
            out.println("                	<td colspan=\"8\">&nbsp;</td>");
          	out.println("                </tr>");
        	out.println("                </table>");
            out.println("               </td>");
            out.println("               <td>");
        	
			sql = "select v.version_id,mp.[year],mp.[month], p.short_name as plant_short_name,p.description as plant_desciption, pv.*";
			sql = "select v.version_id,mp.[year],mp.[month], p.short_name as plant_short_name,p.description as plant_desciption, pv.*, vpvg.total_grinded";
			sql += " FROM 		versions v ";
			sql += " 			inner join dbo.months_periods_versions	mpv	on mpv.version_id = v.version_id"; 
			sql += " 			inner join dbo.plants_versions	pv	on mpv.month_period_version_id = pv.month_period_version_id"; 
			sql += " 			inner join dbo.plants				p	on p.plant_id = pv.plant_id";
			sql += " 			inner join dbo.months_periods			mp	on mp.month_period_id = mpv.month_period_id";
			sql += " 			inner join dbo.vw_plant_version_grind		vpvg	on vpvg.plant_version_id = pv.plant_version_id";
			sql += " where v.version_id = " + versionID; 
			sql += " 	and mp.[year] = " 	+ rs.getInt("current_year"); 
			sql += " 	and mp.[month] = " 	+ rs.getInt("current_month");
			
			out.println("<br> sql [" + sql + "]");

			out.println("                    		<table border=\"1\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\" id=\"tblEnc\" >");
            out.println("                            <tr class=\"colHeader\">");
            out.println("                              <td width=\"300\" >PO</td>");
            out.println("                              <td>Inv.&nbsp;Inicial</td>");
            out.println("                              <td>Compras</td>");
            out.println("                              <td>Disponible</td>");
            out.println("                  			   <td>PPA</td>");
            out.println("                              <td>PSJ</td>");
            out.println("                              <td>PTL</td>");
            out.println("                              <td  NOWRAP>Inv. Final</td>");
            out.println("                            </tr>");
			out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
			out.println("                  <td align=\"center\" colspan=\"8\">"+ rs.getInt("current_year") +" - " + rs.getInt("current_month") +"</td>");
			out.println("                </tr>");
			
			/*
			sql = "select v.version_id,mp.[year],mp.[month], p.short_name as plant_short_name,p.description as plant_desciption, pv.*";
			sql = "select v.version_id,mp.[year],mp.[month], p.short_name as plant_short_name,p.description as plant_desciption, pv.*, vpvg.total_grinded";
			sql += " FROM 		versions v ";
			sql += " 			inner join dbo.months_periods_versions	mpv	on mpv.version_id = v.version_id"; 
			sql += " 			inner join dbo.plants_versions	pv	on mpv.month_period_version_id = pv.month_period_version_id"; 
			sql += " 			inner join dbo.plants				p	on p.plant_id = pv.plant_id";
			sql += " 			inner join dbo.months_periods			mp	on mp.month_period_id = mpv.month_period_id";
			sql += " 			inner join dbo.vw_plant_version_grind		vpvg	on vpvg.plant_version_id = pv.plant_version_id";
			sql += " where v.version_id = " + versionID; 
			sql += " 	and mp.[year] = " 	+ rs.getInt("current_year"); 
			sql += " 	and mp.[month] = " 	+ rs.getInt("current_month");
			*/

			rsPlants = stmtPlants.executeQuery(sql);
			plantsHeader  = new String [10][4];
			int plantsArrayId =0;

			plantsHeader [0][0] = "Programa de molienda"; 
			plantsHeader [1][0] = "Días molienda Inv. Ini.";
			plantsHeader [2][0] = "Almacen";
			plantsHeader [3][0] = "Inv. Inicial Plantas";
			plantsHeader [4][0] = "Inv. Final Plantas";
			plantsHeader [5][0] = "Max Req";
			plantsHeader [6][0] = "Distribuido";
			plantsHeader [7][0] = "Molido";
			plantsHeader [8][0] = "Descarga";
			while (rsPlants.next()){
				if 	(rsPlants.getString("plant_short_name").trim().equals("PPA")) plantsArrayId = 1;
				if 	(rsPlants.getString("plant_short_name").trim().equals("PSJ")) plantsArrayId = 2;
				if 	(rsPlants.getString("plant_short_name").trim().equals("PTL")) plantsArrayId = 3;
				plantsHeader [0][plantsArrayId] = df.format(rsPlants.getDouble("grind") ); 
				plantsHeader [1][plantsArrayId] = df.format(rsPlants.getDouble("grind_protection_days"));
				plantsHeader [2][plantsArrayId] = df.format(rsPlants.getDouble("storage_amount"));
				plantsHeader [3][plantsArrayId] = df.format(rsPlants.getDouble("start_inventory"));
				plantsHeader [4][plantsArrayId] = df.format(rsPlants.getDouble("end_inventory"));
				plantsHeader [5][plantsArrayId] = df.format(rsPlants.getDouble("grind") +  rsPlants.getDouble("storage_amount") - rsPlants.getDouble("start_inventory"));
				plantsHeader [6][plantsArrayId] = df.format(rsPlants.getDouble("received"));
				plantsHeader [7][plantsArrayId] = df.format(rsPlants.getDouble("total_grinded"));
				plantsHeader [8][plantsArrayId] = df.format(rsPlants.getDouble("unload_amount"));

			}
			for (int i = 0; i<9; i++){
				out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
				out.println("                  <td align=\"left\" colspan=\"4\">"+ plantsHeader [i][0] +"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][1]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][2]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][3]+"</td>");
				out.println("                  <td align=\"center\" colspan=\"1\">&nbsp;</td>");				
				out.println("                </tr>");
				odd = !odd;
			}
			out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
			out.println("                  <td align=\"center\" colspan=\"8\">&nbsp;</td>");
			out.println("                </tr>");
			odd = true;
			/*
				Maiz por planta
			*/
			
			sql = "Select * ";
			sql += " from vw_corn_type_distributions "; 
			sql += " where version_id = " + versionID; 
			sql += " 	and [year] = " 	+ rs.getInt("current_year"); 
			sql += " 	and [month] = " 	+ rs.getInt("current_month");
			sql += " order by [year],[month],corn_type_id,plant_id";
			ResultSet rsCornTypeInv = stmtCornInventories.executeQuery(sql);
			out.println("                <tr class=\"colHeader\">" );
			out.println("                  <td align=\"left\" width=\"400\" colspan=\"2\">Maiz</td>");
			out.println("                  <td align=\"left\" colspan=\"2\">&nbsp;</td>");
			out.println("                  <td align=\"right\" >PPA</td>");
			out.println("                  <td align=\"right\" >PSJ</td>");
			out.println("                  <td align=\"right\" >PTL</td>");
			out.println("                </tr>" );
			
			
			cornDistribution[0][0] = "Inv. Inicial";
			cornDistribution[1][0] = "Distribuido";
			cornDistribution[2][0] = "Molido";
			cornDistribution[3][0] = "Inv. Final";
			String prevCornType = "";
			while (rsCornTypeInv.next()){
				if 	(rsCornTypeInv.getString("short_name").trim().equals("PPA")) plantsArrayId = 1;
				if 	(rsCornTypeInv.getString("short_name").trim().equals("PSJ")) plantsArrayId = 2;
				if 	(rsCornTypeInv.getString("short_name").trim().equals("PTL")) plantsArrayId = 3;
				
				if ( (prevCornType.length() > 0) && (!prevCornType.equals(rsCornTypeInv.getString("code")))){
					
					for (int i = 0; i<4; i++){
						out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
						if (i == 0)
							out.println("                  <td align=\"center\" colspan=\"2\">"+ prevCornType +"</td>");
						else
							out.println("                  <td align=\"center\" colspan=\"2\">&nbsp;</td>");
						out.println("                  <td align=\"left\" colspan=\"2\">"+ cornDistribution [i][0] +"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][1]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][2]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][3]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">&nbsp;</td>");
						out.println("                </tr>");
					}
					odd = !odd;
					
				}
				cornDistribution[0][plantsArrayId] = df.format(rsCornTypeInv.getDouble("start_inventory"));
				cornDistribution[1][plantsArrayId] = df.format(rsCornTypeInv.getDouble("distributed"));
				cornDistribution[2][plantsArrayId] = df.format(rsCornTypeInv.getDouble("grinded"));
				cornDistribution[3][plantsArrayId] = df.format(rsCornTypeInv.getDouble("end_inventory"));
				
				prevCornType =  rsCornTypeInv.getString("code");
			}
			for (int i = 0; i<4; i++){
				out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">" );
				if (i == 0)
					out.println("                  <td align=\"center\" colspan=\"2\">"+ prevCornType +"</td>");
				else
					out.println("                  <td align=\"center\" colspan=\"2\">&nbsp;</td>");
				out.println("                  <td align=\"left\" colspan=\"2\">"+ cornDistribution [i][0] +"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][1]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][2]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][3]+"</td>");
				out.println("                  <td align=\"right\" colspan=\"1\">&nbsp;</td>");
				out.println("                </tr>");
			}
				
			odd = true;
	
			
		}
		prevMonth = rs.getInt("current_month");
		prevYear  = rs.getInt("current_year");
		prevPONum = poNum;
		prevBiweekShortName = biweekShortName; 
	}
	
	out.println("                <tr class=\"" + (odd ? "rowOdd" : "rowEven") + "\">");
	out.println("                  <td NOWRAP>" + prevPONum + "&nbsp;" + prevBiweekShortName + "</td>");
	out.println("                  <td align=\"right\">" + (POInitial >0.0 ? df.format(POInitial): "&nbsp;") + "</td>");
	out.println("                  <td align=\"right\">" + (POCapacity >0.0 ? df.format(POCapacity): "&nbsp;") + "</td>");
	out.println("                  <td align=\"right\">" + (POCapacity + POInitial > 0.0 ? df.format(POCapacity + POInitial) : "&nbsp;") + "</td>");
	out.println("                  <td align=\"right\">" + (plantsDistributed[0] > 0.0 ? df.format(plantsDistributed[0]) : "&nbsp;") + "</td>");
	out.println("                  <td align=\"right\">" + (plantsDistributed[1] > 0.0 ? df.format(plantsDistributed[1]) : "&nbsp;") + "</td>");
	out.println("                  <td align=\"right\">" + (plantsDistributed[2] > 0.0 ? df.format(plantsDistributed[2]) : "&nbsp;") + "</td>");
	out.println("                  <td align=\"right\">" + ((POCapacity + POInitial)- PODistributed > 0.0 ? df.format((POCapacity + POInitial)- PODistributed)  : "&nbsp;") + "</td>");
	out.println("                </tr>");
  	out.println("                <tr>");
    out.println("                	<td colspan=\"8\">&nbsp;</td>");
  	out.println("                </tr>");
	out.println("                </table>");
    out.println("               </td>");
    out.println("               <td>");
	out.println("</td>");
	out.println("</tr>");
	out.println("</table>");              
}catch (Exception e){
    out.println("<hr><br>Error Description: " + e.getLocalizedMessage() + "<hr>");
    e.printStackTrace();
} finally {
    try { conn.close(); } catch (Exception e) {}
    try { stmt.close(); } catch (Exception e) {}
    try { rs.close();   } catch (Exception e) {}
}
%>
</body>
</html>


